package com.nip.workmanager.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.nip.workmanagement.R;
import com.nip.workmanager.adapter.IconAdapter;
import com.nip.workmanager.common.ConstantIntentCode;
import com.nip.workmanager.database.TodoTable;
import com.nip.workmanager.entity.IconEntity;
import com.nip.workmanager.entity.TodoEntity;

/**
 * @author ThangNM
 *
 */
public class TodoListActivity extends Activity implements OnClickListener, OnItemClickListener{

	private Button _btnAddTodo;
	private Intent _intent;
	private String _mWorkId;
	private ArrayList<IconEntity> _mListSource;
	private ListView _mLvShow;
	private IconAdapter _mAdapter;
	private TodoTable _mTodoTable;
	private List<TodoEntity> _listSourceTodo;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.todo_list_activity);
		_intent = getIntent();
		Bundle bundle = _intent.getExtras();
		_mWorkId = bundle.getString(ConstantIntentCode.SHOW_LIST_TODO);
		Initialize();
	}

	/**
	 * 
	 */
	private void Initialize()
	{
		_btnAddTodo = (Button)findViewById(R.id.btnAddATodo);
		_btnAddTodo.setOnClickListener(this);
		
		_mListSource = new ArrayList<IconEntity>();
		_mAdapter = new IconAdapter(_mListSource, TodoListActivity.this);
		_mLvShow = (ListView) findViewById(R.id.lvTodoList);
		
		_mTodoTable = new TodoTable(TodoListActivity.this);
		_mLvShow.setAdapter(_mAdapter);
		RefreshDataListView();
		_mLvShow.setOnItemClickListener(this);
	}
	
	/**
	 * 
	 */
	private void RefreshDataListView()
	{
		_listSourceTodo = _mTodoTable.GetToDoByWorkId(_mWorkId);
		if(_listSourceTodo != null)
		{
			_mListSource.clear();
			IconEntity mTodo;
			for (TodoEntity todoEntity : _listSourceTodo) {
				mTodo = new IconEntity();
				mTodo.setIconName(todoEntity.getToDoName());
				if(todoEntity.getToDoStatus())
				{
					mTodo.setIcon(R.drawable.check_icon);
				}
				_mListSource.add(mTodo);
			}
			_mAdapter.notifyDataSetChanged();
		}
	}
	
	/**
	 * 
	 */
	private void AddTodo()
	{
		Intent i = new Intent(TodoListActivity.this,AddToDoActivity.class);
		TodoEntity todo = new TodoEntity();
		todo.setWorkId(_mWorkId);
		Bundle bundle = new Bundle();
		bundle.putBoolean(ConstantIntentCode.REGIST_UPDATE_TODO, true);
		bundle.putSerializable(ConstantIntentCode.TODO_ENTITY, todo);
		i.putExtras(bundle);
		startActivityForResult(i, ConstantIntentCode.REQUEST_ADD_TODO_CODE);
	}
	
	/**
	 * @param index
	 */
	private void UpdateTodo(int index)
	{
		Intent i = new Intent(TodoListActivity.this,AddToDoActivity.class);
		TodoEntity todo = _listSourceTodo.get(index);
		Bundle bundle = new Bundle();
		bundle.putBoolean(ConstantIntentCode.REGIST_UPDATE_TODO, false);
		bundle.putSerializable(ConstantIntentCode.TODO_ENTITY, todo);
		i.putExtras(bundle);
		startActivityForResult(i, ConstantIntentCode.REQUEST_ADD_TODO_CODE);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == ConstantIntentCode.REQUEST_ADD_TODO_CODE)
		{
			if(resultCode == ConstantIntentCode.RESULT_ADD_TODO_CODE)
			{
				Boolean result = data.getBooleanExtra(ConstantIntentCode.REGIST_TODO_RESULT, false);
				if(result)
				{
					RefreshDataListView();
				}
			}
		}
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnAddATodo:
			AddTodo();
			break;

		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		UpdateTodo(arg2);
	}
	
	
}
